package com.company.provider.controller;

import com.company.api.entity.Goods;
import com.company.provider.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/list")
    public List<Goods> getGoods() {
        return goodsService.getGoods();
    }

    /**
     * 服务发现，提供一个接口可以查询当前组件提供了哪些服务
     * @return
     */
    @GetMapping("/discovery")
    public Object discovery() {
        List<String> services = discoveryClient.getServices();
        System.out.println("discovery服务列表" + services);

        List<ServiceInstance> instances = discoveryClient.getInstances("microservicecloud-goods".toUpperCase());
        instances.forEach(x ->
                System.out.println("serviceId:" + x.getServiceId()
        + ",host:" + x.getHost()
        + ",port:" + x.getPort()
        + ",uri:" + x.getUri()));
        return discoveryClient;
    }
}
